package com.example.dmweb.chat.ElectricityPrice;

import com.example.dmdomain.ElectricityPriceKeywordRepository;
import com.example.dmdomain.entity.ElectricityPriceKeyword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ElectricityPriceKeywordService {
    private final ElectricityPriceKeywordRepository keywordRepository;

    @Autowired
    public ElectricityPriceKeywordService(ElectricityPriceKeywordRepository keywordRepository) {
        this.keywordRepository = keywordRepository;
    }
    // 保存关键词
    public ElectricityPriceKeyword saveKeyword(ElectricityPriceKeyword keyword) {
        return keywordRepository.save(keyword);
    }


    //获取所有的关键词
    public List<ElectricityPriceKeyword> getAllKeywords() {
        return keywordRepository.findAll();
    }

    // 随机获取三条关键词数据
    public List<ElectricityPriceKeyword> getRandomKeyword() {
        List<ElectricityPriceKeyword> allKeywords = getAllKeywords();
        if (allKeywords.isEmpty()) {
            return null;
        }
        List<ElectricityPriceKeyword> randomKeywords = new ArrayList<>();
        Random random = new Random();
        Set<Integer> usedIndices = new HashSet<>();
        while (randomKeywords.size() < 3 && usedIndices.size() < allKeywords.size()) {
            int randomIndex = random.nextInt(allKeywords.size());
            if (!usedIndices.contains(randomIndex)) {
                usedIndices.add(randomIndex);
                randomKeywords.add(allKeywords.get(randomIndex));
            }
        }
        return randomKeywords;
    }

















}
